package com.qlm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qlm.entity.Record;
import com.qlm.service.IRecordService;
import com.qlm.tools.Common;
import com.qlm.tools.DateUtils;
import com.qlm.view.RecordView;
import com.qlm.view.ResultMessage;

public class RecordServiceImpl implements IRecordService {

	@Override
	public String getRecordList(String aoData) {
		// 将参数转换成jsonArray类型
				JSONArray jsonarray = JSONArray.parseArray(aoData);
				JSONObject obj = (JSONObject) jsonarray.get(0);
				// 传入的查询参数
				String sSearch = obj.get("sSearch").toString();

				StringBuilder sbSql = new StringBuilder(
						"select id,valid_time,month_batch,number,create_time,create_time,id_begin,id_end,money,valid from t_record ");
				if (sSearch != null && !"".equals(sSearch)) {
					sbSql.append(" where valid_time like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or month_batch like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or create_time like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or id_begin like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or id_end like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or money like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
					sbSql.append(" or number like  CONCAT('%',");
					sbSql.append("'" + sSearch + "'");
					sbSql.append(",'%') ");
				}
				sbSql.append(" order by create_time desc");
				// 分页查询
				List<Record> areaList = Record.dao.find(sbSql.toString());

				List<Object> aList = new ArrayList<Object>();

				if (areaList != null) {
					int index = 1;
					for (Record r : areaList) {
						RecordView av = new RecordView();
						av.setSequenceNumber(index);
						av.setId(r.getInt("id"));
						av.setNumber(r.getInt("number")==null?0:r.getInt("number"));
						av.setMoney(r.getInt("money")==null?0:r.getInt("money"));
						av.setId_begin(r.getInt("id_begin")==null?0:r.getInt("id_begin"));
						av.setId_end(r.getInt("id_end")==null?0:r.getInt("id_end"));
						av.setValid(r.getInt("valid")==null?0:r.getInt("valid"));
						av.setCreate_time(r.getDate("create_time")==null?null:DateUtils.dateToString(r.getDate("create_time"), DateUtils.PATTEN_HMS));
						av.setValid_time(r.getDate("valid_time")==null?null:DateUtils.dateToString(r.getDate("valid_time"), DateUtils.PATTEN_HMS));
						av.setMonth_batch(r.getStr("month_batch")==null?null:r.getStr("month_batch").toUpperCase());
						aList.add(av);
						index++;
					}
				}

		return Common.dataTableSearch(aoData, aList);
	}

	@Override
	public ResultMessage setValid(Integer id, Integer valid) {
		if(!Common.isNullOrEmpty(id) && !Common.isNullOrEmpty(valid)){
			if(valid == 0 || valid == 1){
				Record r = new Record();
				boolean flg = r.set("id", id).set("valid", valid).set("valid_time", DateUtils.dateToString(DateUtils.PATTEN_HMS)).update();
				if(flg){
					return new ResultMessage(1, "修改状态成功", null);
				}
				return new ResultMessage(0, "修改状态失败", null);
			}
		}
		return new ResultMessage(0, "网络连接错误", null);
	}

}
